﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySqlDBhelper;

namespace 高频打印
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
            DbHelperMySQL.connectionString = "data source=localhost;database=laomiao;user id=root;password=root;pooling=false;charset=utf8";

        }
         
        //MySqlConnection conn = new MySqlConnection(DbHelperMySQL.connectionString);
      

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("请选择一个用户进行登陆");
                return;
            }
            if (textBox1.Text =="")
            {
                MessageBox.Show("请选择输入密码进行登陆");
                return;
            }

            string sql = "select * from User_Table where UserID = '" + comboBox1.Text + "'";
            DataTable dtShow = new DataTable();
            //MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
            //sda.Fill(dtShow);
            dtShow = DbHelperMySQL.QuerytoDataTable(sql);
            if (dtShow.Rows.Count == 1)
            {
                if (textBox1.Text  == dtShow.Rows[0][1].ToString())
                {
                    helper.currloginuserid = dtShow.Rows[0][0].ToString();
                    this.Hide();
                    FormSelect FormMain = new FormSelect(); //自己改成登录框名称
                    FormMain.ShowDialog();
                    this.Close();

                }
                else
                {
                    MessageBox.Show("你输入的密码不正确，请重新输入");
                    
                }
            }
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            string sql = "select UserID from User_Table where UserPower=0 or UserPower=1 order by UserID";
            //DataTable dtShow = new DataTable();//20191226注释
            //MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
            //sda.Fill(dtShow);
            DataTable dtShow = DbHelperMySQL.QuerytoDataTable(sql);
            if (dtShow.Rows.Count > 0)
            {
                for (int i = 0; i < dtShow.Rows.Count; i++)
                {
                    comboBox1.Items.Add(dtShow.Rows[i][0]);

                }
            }

            else
            {
                MessageBox.Show("还没有用户数据，请联系管理员进行数据维护");
            }



            //////////////////////////////
            comboBox1.SelectedIndex = 0;
            textBox1.Text = "1";

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                string sql = "select * from User_Table where UserID= '" + comboBox1.Text + "'";
                DataTable dtShow = new DataTable();
                //MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
                //sda.Fill(dtShow);
                 dtShow = DbHelperMySQL.QuerytoDataTable(sql);
                Class1.Power = int.Parse(dtShow.Rows[0][2].ToString());
                Class1.storeIDGroup = dtShow.Rows[0][4].ToString();
                label3.Text = "用户名：" + dtShow.Rows[0][3].ToString();
                label3.Visible = true;
            }
        }
    }
}
